package com.study.talk.module.netty.handler;

import com.study.talk.module.netty.message.Attributes;
import com.study.talk.module.netty.message.Session;
import com.study.talk.module.netty.message.request.SyncMessageRequest;
import com.study.talk.module.service.UserService;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 离线消息处理
 */

@Component
@Slf4j
@ChannelHandler.Sharable
public class SyncRequestHandler extends SimpleChannelInboundHandler<SyncMessageRequest> {

    @Resource
    private UserService userService;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, SyncMessageRequest msg) throws Exception {
        log.info("服务器收到同步缓存消息， req = {}", msg);
        Session session = ctx.channel().attr(Attributes.SESSION).get();
        userService.sendCacheMessage(session.getUserId(), ctx.channel());
    }
}
